package com.mlog.njyjy.ndop.monitor.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mlog.njyjy.ndop.monitor.dao.entity.TMonitorHost;
import com.mlog.njyjy.ndop.monitor.dao.entity.TMonitorHostGroup;
import com.mlog.njyjy.ndop.monitor.dao.entity.TMonitorHostItem;
import com.mlog.njyjy.ndop.monitor.dao.entity.TMonitorItem;
import com.mlog.njyjy.ndop.monitor.dao.service.impl.TMonitorHostGroupServiceImpl;
import com.mlog.njyjy.ndop.monitor.dao.service.impl.TMonitorHostItemServiceImpl;
import com.mlog.njyjy.ndop.monitor.dao.service.impl.TMonitorHostServiceImpl;
import com.mlog.njyjy.ndop.monitor.dao.service.impl.TMonitorItemServiceImpl;
import com.mlog.njyjy.ndop.monitor.pojo.ItemKey;
import com.mlog.njyjy.ndop.share.zabbix.entity.ZabbixItem;
import com.mlog.njyjy.ndop.share.zabbix.method.ZabbixBaseMethod;
import com.mlog.njyjy.ndop.share.zabbix.method.ZabbixHostMethod;
import com.mlog.njyjy.ndop.share.zabbix.method.ZabbixItemMethod;
import com.mlog.njyjy.ndop.share.zabbix.response.ZabbixResponseHost;
import com.mlog.njyjy.ndop.share.zabbix.response.ZabbixResponseHostgroup;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class InitService implements ApplicationRunner {

	@Autowired
	private TMonitorHostServiceImpl hostServ;

	@Autowired
	private TMonitorHostGroupServiceImpl groupServ;

	@Autowired
	private TMonitorHostItemServiceImpl itemServ;

	@Value("${zabbix.url}")
	private String zabbixUrl;

	@Value("${zabbix.user}")
	private String zabbixUser;

	@Value("${zabbix.password}")
	private String zabbixPwd;

	public static final Map<String, TMonitorItem> ZABBIX_ITEM_ENUM = new ConcurrentHashMap<>();

	@Autowired
	private TMonitorItemServiceImpl itemService;

	@Override
	public void run(ApplicationArguments args) {
		try {
			zabbixAuth();
			loadZabbixItemSets();
			syncZabbixServer();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 加载监控项
	 */
	private void loadZabbixItemSets() {
		List<TMonitorItem> items = itemService.list();
		items.forEach(item -> ZABBIX_ITEM_ENUM.put(item.getItemkey(), item));
	}

	/**
	 * 认证
	 */
	private void zabbixAuth() {
		ZabbixBaseMethod.auth(zabbixUrl, zabbixUser, zabbixPwd);
	}

	public void syncZabbixServer() {
		List<ZabbixResponseHostgroup> groupls = ZabbixHostMethod.getHostgroup(null);
		List<TMonitorHost> batchHost = new ArrayList<>();
		List<TMonitorHostGroup> batchGroup = new ArrayList<>();
		List<TMonitorHostItem> batchItem = new ArrayList<>();
		groupServ.update(new UpdateWrapper<TMonitorHostGroup>().set("follow", 0));
		for (ZabbixResponseHostgroup resGroup : groupls) {
			List<ZabbixResponseHost> groupHosts = resGroup.getHosts();
			for (ZabbixResponseHost resHost : groupHosts) {
				TMonitorHost host = hostServ.getById(resHost.getHostid());
				if (host == null)
					host = new TMonitorHost();
				host.setHostid(resHost.getHostid());
				host.setName(resHost.getName());
				host.setIp(resHost.getIp());
				host.setGroupid(resGroup.getGroupid());
				host.setHost(resHost.getHost());
				host.setAvailable(resHost.getAvailable());

				List<ZabbixItem> items = ZabbixItemMethod.queryItemsByHost(host.getHostid(), null);
				for (ZabbixItem item : items) {
					TMonitorHostItem dbitem = new TMonitorHostItem();
					dbitem.setItemid(item.getItemid());
					dbitem.setHostid(item.getHostid());
					dbitem.setItemkey(item.getKey_());
					dbitem.setDescription(item.getDescription());
					dbitem.setFollow(true);
					dbitem.setName(item.getName());
					dbitem.setTemplateid(item.getTemplateid());
					batchItem.add(dbitem);
					if(ItemKey.SYSTEM_INFO.equals(item.getKey_()))
						host.setUname(item.getLastvalue());
				}
				batchHost.add(host);
			}
			TMonitorHostGroup group = groupServ.getById(resGroup.getGroupid());
			if (group == null)
				group = new TMonitorHostGroup();
			group.setGroupid(resGroup.getGroupid());
			group.setName(resGroup.getName());
			group.setFollow(true);
			if (groupHosts.size() == 0)
				group.setFollow(false);
			batchGroup.add(group);
		}
		hostServ.saveOrUpdateBatch(batchHost);
		groupServ.saveOrUpdateBatch(batchGroup);
		itemServ.saveOrUpdateBatch(batchItem);
	}

}
